﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WcfGeebaServer.ContentManage;
using WcfGeebaServer.Users;
using System.Data.SqlClient;

namespace WcfGeebaServer.DBDAL
{
    public static class Data
    {
        public static Member admin = null;
        
        public static GeebaDS.UsersDataTable members;
        public static GeebaDSTableAdapters.UsersTableAdapter usersAdapter;

        public static GeebaDS.ThreadsDataTable threads;
        public static GeebaDSTableAdapters.ThreadsTableAdapter threadsAdapter;

        public static GeebaDS.SubForumsDataTable subForums;
        public static GeebaDSTableAdapters.SubForumsTableAdapter subForumsAdapter;

        public static GeebaDS.LoggedMembersDataTable loggedMembers;
        public static GeebaDSTableAdapters.LoggedMembersTableAdapter loggedMembersAdapter;

        public static GeebaDS.LoggingDataTable logData;
        public static GeebaDSTableAdapters.LoggingTableAdapter logDataAdapter;


        public static GeebaDS.PoliciesDataTable policy;
        public static GeebaDSTableAdapters.PoliciesTableAdapter policyAdapter;

        public static GeebaDS.RulesDataTable rules;
        public static GeebaDSTableAdapters.RulesTableAdapter rulesAdapter;

        public static GeebaDS.PropertiesDataTable properties;
        public static GeebaDSTableAdapters.PropertiesTableAdapter propertiesAdapter;

        public static GeebaDS.ModeratorInfoDataTable moderators;
        public static GeebaDSTableAdapters.ModeratorInfoTableAdapter moderatorsAdapter;

        static Data()
        {
            Data.init();
        }

        public static void init(){
            try
            {
                usersAdapter = new GeebaDSTableAdapters.UsersTableAdapter();
                Data.members = usersAdapter.GetMembers();

                threadsAdapter = new GeebaDSTableAdapters.ThreadsTableAdapter();
                Data.threads = threadsAdapter.GetData();

                subForumsAdapter = new GeebaDSTableAdapters.SubForumsTableAdapter();
                Data.subForums = subForumsAdapter.GetData();

                loggedMembersAdapter = new GeebaDSTableAdapters.LoggedMembersTableAdapter();
                Data.loggedMembers = loggedMembersAdapter.GetData();

                logDataAdapter = new GeebaDSTableAdapters.LoggingTableAdapter();
                Data.logData = new GeebaDS.LoggingDataTable();

                policyAdapter = new GeebaDSTableAdapters.PoliciesTableAdapter();
                Data.policy = policyAdapter.GetData();

                rulesAdapter = new GeebaDSTableAdapters.RulesTableAdapter();
                Data.rules = rulesAdapter.GetData();

                propertiesAdapter = new GeebaDSTableAdapters.PropertiesTableAdapter();
                Data.properties = propertiesAdapter.GetData();

                moderatorsAdapter = new GeebaDSTableAdapters.ModeratorInfoTableAdapter();
                Data.moderators = moderatorsAdapter.GetData();

                Data.admin = null;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void ClearDB()
        {
            string conString = WcfGeebaServer.Properties.Settings.Default.GeebaDBConnectionString;

            SqlConnection conn = new SqlConnection(conString);
            

            try
            {
                conn.Open();
                string query = "TRUNCATE TABLE LoggedMembers; " +
                    "TRUNCATE TABLE Logging; " +
                    "TRUNCATE TABLE ModeratorInfo; " +
                    "TRUNCATE TABLE Policies; " +
                    "TRUNCATE TABLE Properties; " +
                    "TRUNCATE TABLE Rules; " +
                    "TRUNCATE TABLE SubForums; " +
                    "TRUNCATE TABLE Threads; " +
                    "TRUNCATE TABLE Users; ";
                SqlCommand sqlCmd = new SqlCommand(query);
                sqlCmd.Connection = conn;

                sqlCmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
                Data.init();
            }

            
        }
    }
}